<?php
error_reporting(E_ALL);
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
session_start();


function checkForActiveGame() {
    return false;
}

function startGame($playerX, $playerY, $players, $variables) {
        print("Game: ".$_SESSION["game"]);
    $status = $_SESSION["game"]->startGame($playerX, $playerY, $players, $variables);

    // could do validation here
    $gameCreated = true;
    if ($gameCreated) {
        return $status;
    }
    return 0;
}

/**
 * Add a server call to delete a game.
 */
function deleteGame() {

}

function getAgentLocs() {
    $returnArray = array();
    if($_SESSION["game"]) {
        $locs = $_SESSION["game"]->getAgentLocs();
        $size = java_values($locs->size());
        for($i=0; $i < $size; $i++) { 
            $agentLoc = $locs->get($i);
            $lat = java_values($agentLoc->getLat());
            $lng = java_values($agentLoc->getLng());
            $name = java_values($agentLoc->getName());
            $alive = java_values($agentLoc->isAlive());
            $returnArray[] = array($lat, $lng, $name, $alive);
        }
    } else {
        $returnArray = array(array(33.7726113, -84.357596));
    }
    return json_encode($returnArray);
}

function javaAgentToPhp($agent) {
    $name = java_values($agent->getAgentname());
    $javaLikes = $agent->getLikes();
    $likes = array();
    for($i=0; $i < java_values($javaLikes->size()); $i++) { 
        $likes[] = java_values($javaLikes->get($i));
    }
    
    $javaDislikes = $agent->getDislikes();
    $dislikes = array();
    for($i=0; $i < java_values($javaDislikes->size()); $i++) { 
        $dislikes[] = java_values($javaDislikes->get($i));
    }
    
    $javaFriends = $agent->getFriends();
    $friends = array();
    for($i=0; $i < java_values($javaFriends->size()); $i++) { 
        $friends[] = java_values($javaFriends->get($i));
    }

    $alive = java_values($agent->isLiving());

    return array("name" => $name, 
                 "likes" => $likes,
                 "dislikes" => $dislikes,
                 "friends" => $friends,
                 "alive" => $alive);
}

function getAgentDetails($name) {
    $returnArray = array();
    if($_SESSION["game"]) {
        $agent = $_SESSION["game"]->getAgentDetails($name);

        $returnArray = javaAgentToPhp($agent);    
    } else {
        $returnArray = array("name" => $name, 
                       "likes" => array("Turtles"),
                       "dislikes" => array("Dogs"));
    }
    return json_encode($returnArray);
}

function getAgentList() {
    $returnList = array();
    if($_SESSION["game"]) {
        $agents = $_SESSION["game"]->getAgentList();
        for($i=0; $i < java_values($agents->size()); $i++) { 
            $returnList[] = javaAgentToPhp($agents->get($i));
        } 
    }
    return json_encode($returnList);
}

function checkKiller($name) {
    if($_SESSION["game"]) {
        $killer = java_values($_SESSION["game"]->getKillerName());

        if ($killer == $name) {
            return "YOU WIN! <p>You are so smart.</p>";
        }
        return "You lose... The killer was $killer. You guessed $name. <p>Maybe next time?</p>";
    }
    return "error";
}

function askQuestion($name, $questionType) {
    $answer = "";
    if($_SESSION["game"]) {
        $answer = java_values($_SESSION["game"]->askQuestion($name, $questionType));
    } else {
        $answer = "Couldn't connect to server.";
    }

    return $answer;
}

function getLivingRatio() {
    $ratio = "Rogue Agent";
    if($_SESSION["game"]) {
        return java_values($_SESSION["game"]->getLivingRatio());
    }

    return $ratio;
}

function getTotalEnemies() {
    $enemies = 0;
    if($_SESSION["game"]) {
        return java_values($_SESSION["game"]->getTotalEnemies());
    }

    return $enemies;
}

function getQuestion($name) {
    if($_SESSION["game"]) {
        return java_values($_SESSION["game"]->getQuestion($name));
    }
    return "Server error. Try again";
}

function answerQuestion($name, $question, $answer) {
    $serverresponse = 0;
    if($_SESSION["game"]) {
        $serverresponse = java_values($_SESSION["game"]->answerQuestion($name, $question, $answer));
    }
    return json_encode(array("answer" => $serverresponse, "name" => $name));
}
